package webserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ConnectionHandler extends Thread {
	Socket socket;

	// for sending the output to client
	PrintWriter pw;

	// for getting the input from client
	BufferedReader br;

	ConnectionHandler(Socket socket) throws IOException {
		this.socket = socket;
		br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		pw = new PrintWriter(socket.getOutputStream());
		

	}

	@Override
	public void run() {
		try {
			// here we get the request string and give this string to
			// HTTPRequest
			String reqS = "";

			// from br we have to read our request

			/* while (br.ready())
				reqS += (char) br.read(); */
			
			reqS = br.readLine();
			
			System.out.println("[" + reqS + "]");
			
			HTTPRequest request = new HTTPRequest(reqS);

			HTTPResponse response = new HTTPResponse(request);
			System.out.println(" fadshh ");
			//pw.write(response.response.toCharArray());
			pw.write(response.response);
			pw.flush();
			
			socket.close();
			br.close();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
